<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-     Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

if ($_GET['id']) 
	$id=$_GET['id'];
else 
	$id=$_POST['id'];

$cover_path=get_coverspath();
$sw=$_POST['sw'];

if (!$id) {
	include("includes/search.inc");
}
else {
    if ($sw!=1) {
    	$row = get_book($id);
		if (!$row) {
			print "PROBLEME : " .biblio_db_error();
		}
		else {
			$liv_id = $row['id'];
           	$liv_nom =  $row['nom'];
           	$liv_auteur =  $row['auteur'];
            $liv_genre_ID =  $row['genre_ID'];
           	$liv_local_ID =  $row['location_ID'];
           	$liv_etat_ID =  $row['etat_ID'];
           	$liv_ISBN =  $row['ISBN'];
           	$liv_num =  $row['num'];
            $liv_ed = $row['edition'];
           	$liv_date = $row['date_ed'];
           	$liv_col = $row['collection'];
           	$liv_temp = $row['temp'];
           	$liv_noout = $row['noout'];
           	$liv_resume = $row['resume'];
           	$liv_motclefs = $row['motclefs'];
			if (isset($row['cover_url']))
				$cover_url = $row['cover_url'];
			else 
				$cover_url= "";
			$img_path = $cover_path.$id.".jpg";
		}
?>

<script type="text/javascript" src="js/bsn.AutoSuggest_2.1.3_min.js" charset="utf-8"></script>
<link rel="stylesheet" href="css/autosuggest_inquisitor_min.css" type="text/css" media="screen" charset="utf-8" />

        <CENTER><H3><?php echo _("Modifier le livre"); ?></H3>
        <FORM enctype="multipart/form-data" action="admin.php" method="POST" id="form1" name="form1">
        <TABLE BORDER="0">
        <tr>
			<td><?php echo _("Titre"); ?> : </td>
			<td><input type="text" name="liv_nom" id="liv_nom" size="40" value="<?php echo $liv_nom; ?>" /></td>
			<td rowspan="7">
<?php
	if (file_exists($img_path))
		echo '<img src="images/covers/'.$id.'.jpg" name="liv_img" id="liv_img" align="right"/>';
	else {
		if (!empty($cover_url))
			echo '<img src="'.$cover_url.'" name="liv_img" id="liv_img" align="right"/>';
		else
			echo '<img src="images/livre.jpg" name="liv_img" id="liv_img" align="right"/>';
	}
?>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Num&eacute;ro"); ?> : </td>
			<td><input type="text" name="liv_num" id="liv_num" size="4" value="<?php echo $liv_num; ?>" /></td>
		</tr>
        <tr>
			<td><?php echo _("Auteur"); ?> : </td>
			<td><input type="text" name="liv_auteur" id="liv_auteur" size="40" value="<?php echo $liv_auteur; ?>"/></td>
		</tr>
        <tr>
			<td><?php echo _("Collection"); ?> : </td>
			<td><input type="text" name="liv_col" id="liv_col" size="40" value="<?php echo $liv_col; ?>"/></td>
		</tr>
        <tr>
			<td><?php echo _("Edition"); ?> : </td>
			<td><input type="text" name="liv_ed" id="liv_ed" size="30" value="<?php echo $liv_ed; ?>"/></td>
		</tr>
        <tr>
			<td><?php echo _("Date"); ?> : </td>
			<td><input type="text" name="liv_date" id="liv_date" size="10" value="<?php echo $liv_date; ?>"/></td>
		</tr>
        <tr>
			<td><?php echo _("Genre"); ?> : </td>
			<td>
				<select NAME = "liv_genre">
<?php
	$genres = get_genres();
	if ($genres) {
		foreach ($genres as $key => $value) {
			print "<option VALUE=\"$key\"";
			if ($key == $liv_genre_ID)
				echo ' selected="selected"';
			echo ">".$value."</option>";
		}
	}
?>
				</select>
			</td>
		</tr>
        <tr>
			<td><?php echo _("ISBN"); ?> : 	</td>
			<td>
				<input type="text" name="liv_ISBN" id="liv_ISBN" size="13" value="<?php echo $liv_ISBN; ?>"/>
<?php if (isAmazonEnabled()) { ?>
				<input type="button" value="<?php echo _("Recherche Amazon"); ?>" onClick='document.jsap.searchIsbn(document.forms["form1"].liv_ISBN.value);'/>
<?php } ?>
			</td>
			<td>
<?php if (isAmazonEnabled()) { ?>
				<applet  name="OpenBiblio Isbn Search Applet" id="jsap"
					code="com.biglol.openbiblio.IsbnSearchApplet" 
					archive="applet/ProductAdvertisingAPI.jar, applet/lib/netscape.jar" width="16" height="16" MAYSCRIPT >
					<param name="awsAccessKeyId" value="<?php echo getAmazonKeyId(); ?>"/>
					<param name="awsSecretKey" value="<?php echo getAmazonSecret(); ?>"/>
					<param name="awsAssociateTag" value="<?php echo getAmazonAssociateTag(); ?>"/>
				</applet>
<?php } ?>
			</td>
        </tr>
        <tr>
			<td><?php echo _("R&eacute;sum&eacute;"); ?> : </td>
			<td colspan="2">
				<textarea name="liv_resume" cols="50" rows="5"><?php echo "$liv_resume"; ?></textarea>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Mots cl&eacute;"); ?> : </td>
			<td colspan="2">
				<input type="text" name="liv_motclefs" id="liv_motclefs" size="40" maxlength="200" value="<?php echo $liv_motclefs; ?>"/>
				<small>(<?php echo _("s&eacute;par&eacute;s par un espacement"); ?>)</small>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Etat"); ?> : </td>
			<td colspan="2">
				<select name="liv_etat">
        <?php
        $requete = "SELECT * FROM etats order by value, etat"; 
	if(!($resultat = biblio_db_query($requete)))
		print "PROBLEME : " .biblio_db_error();
	else 
	{
		while ($row = biblio_db_fetch_array($resultat)) 
		{      
		        $etat_id2 = $row['id'];           
			$etat_l =  $row['etat'];                                
			if ($liv_etat_ID==$etat_id2) { $str = "selected"; }
			else { $str="";}
			print "<option VALUE=\"$etat_id2\" $str>$etat_l";
		}
	}                                
        ?>
				</select>
			</td>
		</tr>
        <tr>
			<td><?php echo _("Location"); ?> : </td>
			<td colspan="2">
				<select NAME = "liv_location">
<?php
	$locations = get_locations();
	if ($locations) {
		foreach ($locations as $id => $location) {
			if ($liv_local_ID==$id)
				echo '<option VALUE="'.$id.'" selected="selected">';
			else
				echo '<option VALUE="'.$id.'">';
			echo $location.'</option>';
		}
	}                                  
?>
				</select>
			</td>
		</tr>
        <tr>
	        <td><?php echo _("Temporaire"); ?> :</td>
			<td colspan="2"><INPUT type="checkbox" name="temp" value="1" <?php if($liv_temp == 1) echo "checked"; ?> /></td>
		</tr>
        <tr>
			<td><?php echo _("Ne peux sortir !"); ?> : </td>
			<td colspan="2"><input type="checkbox" name="noout" value="1" <?php if($liv_noout == 1) echo "checked"; ?> /></td>
		</tr>
		<tr>
			<td><?php echo _("Couverture"); ?> : </td>
			<td colspan="2">
				<input type="file" name="liv_cover"/>
				<i><?php echo _("fichier"); ?>	</i><br>
				<input type="text" size="40" name="liv_url" id="liv_url" value="<?php echo $cover_url; ?>"/>
				<i><?php echo _("ou url"); ?></i>
			</td>
		</tr>
	</TABLE>
	<BR>
	<input type="hidden" NAME="user" value="<?php echo $user; ?>"/>
	<input type="hidden" NAME="action" value="<?php echo $action; ?>"/>
	<input type="hidden" NAME="type" value="<?php echo $type; ?>"/>
    <input type="hidden" NAME="sw" value="1"/>
	<input type="hidden" NAME="id" value="<?php echo $liv_id; ?>"/>        
	<input type="Submit" value="<?php echo _("Modifier"); ?>"/>
</FORM>
</CENTER>
        <?php
    }   
    else {   
		$temp=$_POST['temp'];
		$noout=$_POST['noout'];
		$liv_resume=$_POST['liv_resume'];
		$liv_motclefs=$_POST['liv_motclefs'];
		$liv_nom= format_title($_POST['liv_nom']);
		$liv_num=$_POST['liv_num'];
		$liv_auteur=format_author($_POST['liv_auteur']);
		$liv_col=$_POST['liv_col'];
		$liv_genre=$_POST['liv_genre'];
		$liv_location=$_POST['liv_location'];
		$liv_ed=$_POST['liv_ed'];
		$liv_etat=$_POST['liv_etat'];
		$liv_ISBN=$_POST['liv_ISBN'];
		$liv_date=$_POST['liv_date'];
		$cover_url = $_POST['liv_url'];

		if($temp != 1) 
			$temp = 0;
		if ($noout != 1) 
			$noout = 0;
		$requete = "update livres set resume='$liv_resume', motclefs='$liv_motclefs', nom='$liv_nom', auteur='$liv_auteur', collection='$liv_col', genre_ID=$liv_genre, location_ID=$liv_location, edition='$liv_ed', etat_ID=$liv_etat, ISBN='$liv_ISBN', temp=$temp, noout=$noout, date_ed='$liv_date', num='$liv_num', cover_url='$cover_url' where id=$id";
		if(!($resultat = biblio_db_query($requete))) {
			echo "PROBLEME : $requete: " .biblio_db_error(); 
			exit;
		}  
		put_log(3,$requete,$user);
		echo "<CENTER><BR>";
		echo _("Le livre a &eacute;t&eacute; modifi&eacute;.");
		echo "<br></center>";
		$filename = $id . ".jpg";
		if ($_FILES['liv_cover']['tmp_name']) {
			move_uploaded_file($_FILES['liv_cover']['tmp_name'], $cover_path.$filename);
		}
	} 
}
?>
<br/><br/>
<div>
	<span id=left-text>
		<a href="admin.php?menu=1"><?php echo _("retour menu administration"); ?></a><BR>
		<a href="index.php"><?php echo _("retour menu principal"); ?></a>
	</span>
</div>
<br/>

<script language="javascript">
	function isEmpty(str) {
		if (str.length > 0)
			return false;
		else
			return true;
	}
	
	function updateTitle(s) {
		if (isEmpty(document.forms["form1"].liv_nom.value ))
			document.forms["form1"].liv_nom.value = s;
	}
	function updateAuthor(s) {
		if (isEmpty(document.forms["form1"].liv_auteur.value))
			document.forms["form1"].liv_auteur.value = s;
	}
	function updatePublisher(s) {
		if (isEmpty(document.forms["form1"].liv_ed.value))
			document.forms["form1"].liv_ed.value = s;
	}
	function updatePubDate(s) {
		if (isEmpty(document.forms["form1"].liv_date.value))
			document.forms["form1"].liv_date.value = s;
	}
	function updateCoverUrl(s) {
		document.forms["form1"].liv_url.value = s;
		document.forms["form1"].liv_img.src = s;
	}
	
	window.onload = function() {
		new biblioSuggest('liv_nom', 2500);
		new biblioSuggest('liv_auteur', 2500);
		new biblioSuggest('liv_ISBN', 2500);
		new biblioSuggest('liv_ed', 2500);
		new biblioSuggest('liv_col', 2500);
    }

</script>
